﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;
using System.Data;
using SMS.Entities.Master;

namespace SMS.DAL
{
    public class PurchaseDA : BaseDA<StockMaster>
    {


        public override StockMaster SaveObject(string userName, StockMaster tObject)
        {
            /*
             * USE [Shop]
GO
****** Object:  StoredProcedure [dbo].[USP_INSERT_UPDATE_STOCKMASTER]    Script Date: 05/18/2012 17:11:59 ******
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[USP_INSERT_UPDATE_STOCKMASTER]
(@Stock_ID int,
@Stock_Date date,
@Supplier int,
@Date date,
@TotalAmount decimal(18,2),
@PaidAmount decimal(18,2),
@Extra_Charge decimal(18,2),
@IsLive bit)
as
if(@Stock_ID=0)
begin
	INSERT INTO [Shop].[dbo].[StockMaster]
			   ([Stock_Date]
			   ,[Supplier]
			   ,[Date]
			   ,[TotalAmount]
			   ,[PaidAmount]
			   ,[Extra_Charge]
			   ,[IsLive])
		 VALUES
			   (@Stock_Date
			   ,@Supplier
			   ,@Date
			   ,@TotalAmount
			   ,@PaidAmount
			   ,@Extra_Charge
			   ,@IsLive)
	SELECT @@IDENTITY
end
else
begin
	UPDATE [Shop].[dbo].[StockMaster]
	   SET [Stock_Date] = @Stock_Date
		  ,[Supplier] = @Supplier
		  ,[Date] = @Date
		  ,[TotalAmount] = @TotalAmount
		  ,[PaidAmount] = @PaidAmount
		  ,[Extra_Charge] = @Extra_Charge
		  ,[IsLive] = @IsLive
	 WHERE Stock_ID=@Stock_ID
end*/
            try
            {
                dbHelper.AddParameter("@Stock_ID", tObject.Stock_ID);
                dbHelper.AddParameter("@Supplier", tObject.Supplier.Supplier_ID);
                dbHelper.AddParameter("@Branch", tObject.Branch.Branch_ID);
                dbHelper.AddParameter("@Stock_Date", tObject.Stock_Date);
                dbHelper.AddParameter("@Date", tObject.Date);
                dbHelper.AddParameter("@TotalAmount", tObject.TotalAmount);
                dbHelper.AddParameter("@PaidAmount", tObject.PaidAmount);
                dbHelper.AddParameter("@Extra_Charge", tObject.Extra_Charge);
                dbHelper.AddParameter("@IsLive", tObject.IsLive);
                object objectID = dbHelper.ExecuteScaler("USP_INSERT_UPDATE_STOCKMASTER", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (Convert.ToInt32(objectID) > 0)
                    tObject.Stock_ID = Convert.ToInt32(objectID);
                LogActivity(userName, string.Format("StockMaster Saved with ID {0}", tObject.Stock_ID), tObject);
                return tObject;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, tObject);
                throw new Exception("Error Occured while Saving StockMaster");
            }
        }

        public override bool DeleteObject(string userName, StockMaster tObject)
        {
            throw new NotImplementedException();
        }

        public override List<StockMaster> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            try
            {
                dbHelper.AddParameter("@Stock_Id", 0);
                dbHelper.AddParameter("@FROMDATE", fromDate);
                dbHelper.AddParameter("@TODATE", toDate);
                IDataReader dr = dbHelper.ExecuteReader("USP_SELECT_STOCKMASTER", CommandType.StoredProcedure, ConnectionState.Closed);
                var list = new List<StockMaster>();
                while (dr.Read())
                {
                    list.Add(GetStockMasterObject(dr, false));
                }
                dr.Close();
                LogActivity(userName, string.Format("StockMaster loaded from {0} to {1}", fromDate, toDate), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw;
            }
            #region
            /*
             CREATE PROCEDURE USP_SELECT_STOCKMASTER
(@Stock_ID int,
@FROMDATE DATE,
@TODATE DATE)
AS
IF (@Stock_ID>0)
BEGIN
	SELECT [Stock_ID]
      ,[Stock_Date]
      ,[Supplier] AS SUPPLIER_ID
      ,S.Supplier_Name
      ,[Date]
      ,[TotalAmount]
      ,[PaidAmount]
      ,[Extra_Charge]
      ,[StockMaster].[IsLive]      
  FROM [StockMaster] JOIN [Supplier] S ON 
  StockMaster.Supplier=S.Supplier_ID
  WHERE Stock_ID=@Stock_ID
  END
ELSE
BEGIN
	SELECT [Stock_ID]
      ,[Stock_Date]
      FROM StockMaster
      WHERE [Stock_Date]> @FROMDATE AND [Stock_Date]<@TODATE
END
             * */
            #endregion
        }

        public StockMaster GetCompleteObject(string userName, int StockID)
        {
            try
            {
                dbHelper.AddParameter("@Stock_Id", StockID);
                dbHelper.AddParameter("@FROMDATE", DateTime.Now);
                dbHelper.AddParameter("@TODATE", DateTime.Now);
                IDataReader dr = dbHelper.ExecuteReader("USP_SELECT_STOCKMASTER", CommandType.StoredProcedure, ConnectionState.Closed);
                StockMaster obj = null;
                if (dr.Read())
                    obj = GetStockMasterObject(dr, true);
                dr.Close();
                LogActivity(userName, string.Format("Full StockMaster Loaded ID: {0}", StockID), obj);
                return obj;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw;
            }

        }

        private StockMaster GetStockMasterObject(IDataReader dr, bool isFull)
        {
            StockMaster obj = new StockMaster();
            int i = 0;
            obj.Stock_ID = dr.GetInt32(i++);
            obj.Stock_Date = dr.GetDateTime(i++);
            if (isFull)
            {
                obj.Supplier = new Supplier { Supplier_ID = dr.GetInt32(i++), Supplier_Name = dr.GetString(i++) };
                if (!dr.IsDBNull(4) && !dr.IsDBNull(5))
                    obj.Branch = new Branch { Branch_ID = dr.GetInt32(i++), BranchName = dr.GetString(i++) };
                obj.Date = dr.GetDateTime(i++);
                obj.TotalAmount = dr.GetDecimal(i++);
                obj.PaidAmount = dr.GetDecimal(i++);
                obj.Extra_Charge = dr.GetDecimal(i++);
                obj.IsLive = dr.GetBoolean(i++);
            }
            return obj;
        }

        public override List<StockMaster> SearchObject(string userName, string SearchText)
        {
            throw new NotImplementedException();
        }
    }
}
